package rladidxo.basic;

import java.util.Scanner;

public class Month {
	int daysPerMonth;
	int firstDay;
	int firstDayOfNextMonth;


	public Month(int daysPerMonth, int firstDay) {
	this.daysPerMonth = daysPerMonth;
	this.firstDay = firstDay;
	}


	public void printMonth() {
	int cnt = 1;


	System.out.printf("%3s%3s%3s%3s%3s%3s%3s\n", "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
	System.out.printf("%3s%3s%3s%3s%3s%3s%3s\n", "--", "--", "--", "--", "--", "--", "--");


	for(int i=1 ; cnt<=daysPerMonth ; i++) {
	if( i <= firstDay )
	System.out.printf("%3s", " ");
	else
	System.out.printf("%3s", cnt++);


	if( i % 7 == 0)
	System.out.println();
	}
	}


	public int getFirstDayOfNextMonth() {
	return (firstDay + daysPerMonth) % 7;
	}
	}


	public class Calendar
	{
	static final int FEBRUARY        = 2;
	static final int MONTHS_PER_YEAR = 12;
	

	static int [] daysPerMonth =
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


	static String [] months =
	{"", "  Januray", "February", "   March", "   April",
	"      May", "    June", "    July", "  August",
	"September", " October", "November", "December"};


	public static void main(String[] args)
	{
	Scanner in = new Scanner(System.in);
	System.out.print("Enter the year? (yyyy): ");
	int year = in.nextInt();
	int firstDay = firstDayOfYear(year);


	// February: Check for leap year
	daysPerMonth [FEBRUARY] = isLeapYear(year) ? 29 : 28;


	for (int i=1; i <= MONTHS_PER_YEAR; i++) {
	Month month = new Month(daysPerMonth[i], firstDay);
	System.out.println("\n\n      "+months[i]+" "+year);
	month.printMonth();
	firstDay = month.getFirstDayOfNextMonth();
	}
	}
	

	private static boolean isLeapYear(int year) {
	return ((year%4 == 0) && (year%100 != 0)) || (year%400 == 0);
	}
	private static int firstDayOfYear(int year) {
	return (2 * (13) + (3 * (13+1)/5) + (year-1) + ((year-1)/4)
	- ((year-1)/100) + ((year-1)/400) + 2) % 7;
	}
	}